App.controller("productClassCtrl",function($scope, $rootScope, $location, $http, $compile, $stateParams, $timeout, path, publicService) {
	
	$scope.form_1 = {};		//行业分类
	$scope.form_2 = {};		//品类分类
	$scope.form_3 = {};		//品牌名称
	var showClassInfo = {
			industry_id: null,
			category_id: null,
			brand_id: null
	};
	
	var limitParam_1 = {imageSize:20, format:"jpg"};		//品类图
	var limitParam_2 = {imageSize:80, format:"jpg/png"};	//示意图
	var alertModel = {};	//alert模态框
	
	//获取三种分类数据
	$scope.getClassData = function(type,parentId){ 
		$http({
			   url:	path + "/server/queryproductcategory/"+type+"/"+parentId,		//1:行业，2:品类，3:品牌
			   method: 'GET' 
		}).success(function(data){
			if(data.code==0){
				var classData = data.result;
				
				$scope.showData(type,classData);
			}else{
				console.log("失败！");
			}
		}).error(function(data){
			console.warn(data);   
		})
	}
	
	//显示数据
	$scope.showData = function(type,dataList){
		var html = '';
		
		$.each(dataList, function(index, OneObj){
			var item_id = type+"_"+OneObj.id
			var image_html = '';
			var delete_html = '';
			if(type==2 || type==3){
				image_html = ('<div class="floatLeft">'
								 +'<img class="classImage" src="'+OneObj.simgurl+'">'
							 +'</div>');
				delete_html = ('<div class="floatRight" ng-click="deleteClass('+OneObj.id+')"><i class="fa fa-times"></i></div>');
			}
			
			html += ('<div class="oneClass">'
						+'<div class="left">'
							+image_html
							+'<div class="floatRight content" id="'+item_id+'" ng-click="chooseClass('+type+','+OneObj.id+')">'+OneObj.name+'</div>'
						+'</div>'
						+'<div class="right">'
							+delete_html
							+'<div class="floatRight" ng-click="editClass('+type+',\''+encodeURI(JSON.stringify(OneObj))+'\')"><i class="fa fa-pencil-square-o"></i></div>'
						+'</div>'
					+'</div>');
		});
		
		html = $compile(html)($scope);   //angularJs代码需要动态编译
		$("#classContainer_"+type).html(html);
		
		if(dataList.length>0){
			$scope.chooseClass(type,dataList[0].id);
		}else{
			$("#classContainer_"+(type+1)).html(html);
			
			if(type==1){		//行业分类
				showClassInfo.industry_id = null;
				showClassInfo.category_id = null;
				showClassInfo.brand_id = null;
			}else if(type==2){	//品类分类
				showClassInfo.category_id = null;
				showClassInfo.brand_id = null;
			}else if(type==3){	//品牌分类
				showClassInfo.brand_id = null;
			}
		}
	}
	
	//选择
	$scope.chooseClass = function(type,id){
		var divId = type+"_"+id;
		angular.element("#"+divId).parent("div").parent("div").parent("div").children("div").removeClass("choose");
		angular.element("#"+divId).parent("div").parent("div").addClass("choose");	
		
		if(type==1){		//行业分类
			showClassInfo.industry_id = id;
			$scope.getClassData(type+1,id);
		}else if(type==2){	//品类分类
			showClassInfo.category_id = id;
			$scope.getClassData(type+1,id);
		}else if(type==3){	//品牌分类
			showClassInfo.brand_id = id;
		}
	}
	
	//添加分类
	$scope.addClass = function(type){
		if(type==1){		//行业分类
			$scope.form_1 = {};
		}else if(type==2){	//品类分类
			$scope.form_2 = {};
			publicService.removeUploadImage("image1_img","image1_url");
			publicService.removeUploadImage("image2_img","image2_url");
			$("#colorRBG").val("#000000");
		}else if(type==3){	//品牌分类
			$scope.form_3 = {};
			publicService.removeUploadImage("image3_img","image3_url");
			
			if(!showClassInfo.category_id){
				alertModel.pannel = jqueryAlert({
					content: "请先创建品类！",
					modal: true
				});
				return;
			}
		}
		$scope["classWindow_"+type].center().open();   //打开弹框
	}
	
	//编辑分类
	$scope.editClass = function(type,objStr){
		var data = JSON.parse(decodeURI(objStr));
		
		if(type==1){		//行业分类
			$scope.form_1.id = data.id;
			$scope.form_1.name = data.name;
		}else if(type==2){	//品类分类
			$scope.form_2.id = data.id;
			$scope.form_2.name = data.name;
			publicService.showUploadImage("image1_img","image1_url",data.simgurl);
			publicService.showUploadImage("image2_img","image2_url",data.bimgurl);
			$("#colorRBG").val("#000000");
		}else if(type==3){	//品牌分类
			$scope.form_3.id = data.id;
			$scope.form_3.name = data.name;
			publicService.showUploadImage("image3_img","image3_url",data.simgurl);
		}
		
		$scope["classWindow_"+type].center().open();   //打开弹框
	}
	
	//删除分类
	$scope.deleteClass = function(id){
		alertModel.confirm = jqueryAlert({
			content: "确定删除？",
			modal: true,
			width: "300",
			"buttons" :{
				"确定" : function(){
					alertModel.confirm.close();
					return;
					
					$http({
						   url:	path + "/server/deleteproductcategory/"+id,		
						   method: 'GET' 
					}).success(function(data){
						alertModel.pannel = jqueryAlert({
							content: data.message,
							modal: true
						});
					}).error(function(data){
						console.warn(data);   
					})
				},
				"取消" : function(){
					alertModel.confirm.close();
				}
			}
		});
	}
	
	//创建分类
	$scope.createClass = function(type){
		var form = {};
		if(type==1){		//行业分类
			form = $scope.form_1;
			form.p_id = 0;
			
			if(!form.name){
				alertModel.pannel = jqueryAlert({
					content: "请输入行业名称！",
					modal: true
				});
				return;
			}
		}else if(type==2){	//品类分类
			form = $scope.form_2;
			form.simgurl = $("#image1_url").val();
			form.bimgurl = $("#image2_url").val();
			form.color_value = $("#colorRBG").val();
			form.p_id = showClassInfo.industry_id;
			
			if(!form.name){
				alertModel.pannel = jqueryAlert({
					content: "请输入品类名称！",
					modal: true
				});
				return;
			}
			if(!form.simgurl){
				alertModel.pannel = jqueryAlert({
					content: "请上传品类图！",
					modal: true
				});
				return;
			}
			if(!form.bimgurl){
				alertModel.pannel = jqueryAlert({
					content: "请上传示意图！",
					modal: true
				});
				return;
			}
			if(!form.color_value){
				alertModel.pannel = jqueryAlert({
					content: "请选择色值！",
					modal: true
				});
				return;
			}
		}else if(type==3){	//品牌分类
			form = $scope.form_3;
			form.simgurl = $("#image3_url").val();
			form.p_id = showClassInfo.category_id;
			
			if(!form.name){
				alertModel.pannel = jqueryAlert({
					content: "请输入品牌名称！",
					modal: true
				});
				return;
			}
			if(!form.simgurl){
				alertModel.pannel = jqueryAlert({
					content: "请上传品类图！",
					modal: true
				});
				return;
			}
		}
		form.type = type;
		
		var url = '';
		if(!form.id){
			url = path+'/server/addproductCategory';
		}else{
			url = path+'/server/modifyproductCategory';
		}
		
		$http({
			   url: url,
			   method: 'POST',   
			   data: angular.toJson(form)
		}).success(function(data){
			if(data.code==0){
				$scope["classWindow_"+type].close();
				alertModel.pannel = jqueryAlert({
					content: "成功！",
					modal: true
				});
				
				if(type==1){		//行业分类
					$scope.getClassData(1,-1);
				}else if(type==2){	//品类分类
					$scope.getClassData(2,form.p_id);
				}else if(type==3){	//品牌分类
					$scope.getClassData(3,form.p_id);
				}
			}else{
				alertModel.pannel = jqueryAlert({
					content: "失败！",
					modal: true
				});
			}
		}).error(function(data){
			console.warn(data);   
		})
		
	}
	
	
	
	
	$scope.getClassData(1,-1);
	
	publicService.uploadImage("image1","image1_img","image1_url",limitParam_1);
	publicService.uploadImage("image2","image2_img","image2_url",limitParam_2);
	publicService.uploadImage("image3","image3_img","image3_url",limitParam_1);

	
	
	
});